package com.elong.qadev.tool;

public class ConvertUtil {
	public static final int ALIGNMENT_HIGHT = 0;
	public static final int ALIGNMENT_LOW = 1;

	public static long byteArrayToLong(byte[] data, int length, int alignment)
			throws Exception {
		long value = 0L;

		switch (alignment) {
		case 0:
			for (int i = 0; i < length; i++) {
				value <<= 8;
				value += toInt(data[i]);
			}
			break;
		case 1:
			for (int i = 0; i < length; i++) {
				value <<= 8;
				value += toInt(data[(length - i - 1)]);
			}
			break;
		default:
			throw new Exception("转换错误");
		}

		return value;
	}

	public static byte[] longToByteArray(long value, int length, int alignment)
			throws Exception {
		byte[] data = new byte[length];

		switch (alignment) {
		case 0:
			for (int i = 0; i < length; i++) {
				data[(length - i - 1)] = (byte) (int) (value & 0xFF);
				value >>>= 8;
			}
			break;
		case 1:
			for (int i = 0; i < length; i++) {
				data[i] = (byte) (int) (value & 0xFF);
				value >>>= 8;
			}
			break;
		default:
			throw new Exception("转换错误");
		}

		return data;
	}

	public static int byteArrayToInt(byte[] data, int length, int alignment)
			throws Exception {
		int value = 0;

		switch (alignment) {
		case 0:
			for (int i = 0; i < length; i++) {
				value <<= 8;
				value += toInt(data[i]);
			}
			break;
		case 1:
			for (int i = 0; i < length; i++) {
				value <<= 8;
				value += toInt(data[(length - i - 1)]);
			}
			break;
		default:
			throw new Exception("转换错误");
		}

		return value;
	}

	public static byte[] intToByteArray(int value, int length, int alignment)
			throws Exception {
		byte[] data = new byte[length];

		switch (alignment) {
		case 0:
			for (int i = 0; i < length; i++) {
				data[(length - i - 1)] = (byte) (value & 0xFF);
				value >>>= 8;
			}
			break;
		case 1:
			for (int i = 0; i < length; i++) {
				data[i] = (byte) (value & 0xFF);
				value >>>= 8;
			}
			break;
		default:
			throw new Exception("转换错误");
		}

		return data;
	}

	private static int toInt(byte b) {
		if (b >= 0) {
			return b;
		}
		return b + 256;
	}

	public static float byteArrayToFloat(byte[] data, int alignment)
			throws Exception {
		int value = byteArrayToInt(data, 4, alignment);

		return Float.intBitsToFloat(value);
	}

	public static byte[] floatToByteArray(float value, int alignment)
			throws Exception {
		int i = Float.floatToIntBits(value);

		return intToByteArray(i, 4, alignment);
	}

	public static double byteArrayToDouble(byte[] data, int alignment)
			throws Exception {
		long value = byteArrayToLong(data, 8, alignment);

		return Double.longBitsToDouble(value);
	}

	public static byte[] doubleToByteArray(double value, int alignment)
			throws Exception {
		long l = Double.doubleToLongBits(value);

		return longToByteArray(l, 8, alignment);
	}

	public static String bcdToString(byte[] data, int length, int alignment)
			throws Exception {
		StringBuffer str = new StringBuffer();

		boolean flag = false;
		int value;
		for (int i = 0; i < length; i++) {
			switch (alignment) {
			case 0:
				value = (data[i] & 0xF0) >>> 4;
				if ((value >= 0) && (value <= 9)) {
					if ((!flag) && (value > 0)) {
						flag = true;
					}

					if (flag) {
						char ch = '0';
						ch = (char) (ch + value);
						str.append(ch);
					}
				} else {
					throw new Exception("转换错误");
				}
				value = data[i] & 0xF;
				if ((value >= 0) && (value <= 9)) {
					if ((!flag) && (value > 0)) {
						flag = true;
					}

					if (!flag)
						continue;
					char ch = '0';
					ch = (char) (ch + value);
					str.append(ch);
				} else {
					throw new Exception("转换错误");
				}

			case 1:
				value = (data[(length - 1 - i)] & 0xF0) >>> 4;
				if ((value >= 0) && (value <= 9)) {
					if ((!flag) && (value > 0)) {
						flag = true;
					}

					if (flag) {
						char ch = '0';
						ch = (char) (ch + value);
						str.append(ch);
					}
				} else {
					throw new Exception("转换错误");
				}
				value = data[(length - 1 - i)] & 0xF;
				if ((value >= 0) && (value <= 9)) {
					if ((!flag) && (value > 0)) {
						flag = true;
					}

					if (!flag)
						continue;
					char ch = '0';
					ch = (char) (ch + value);
					str.append(ch);
				} else {
					throw new Exception("转换错误");
				}

			default:
				throw new Exception("转换错误");
			}
		}

		return str.toString();
	}

	public static byte[] stringToBcd(String value, int length, int alignment)
			throws Exception {
		byte[] data = (byte[]) null;

		if (value != null) {
			byte[] bytes = value.getBytes();
			if (length <= 0) {
				length = (bytes.length + 1) / 2;
			}
			data = new byte[length];
			int index = bytes.length - 1;
			for (int i = 0; i < length; i++) {
				int b = 0;
				if (index >= 0) {
					if ((bytes[index] >= 48) && (bytes[index] <= 57)) {
						b = bytes[index] - 48;
						index--;
					} else {
						throw new Exception("转换错误");
					}
				}
				switch (alignment) {
				case 0:
					int tmp127_126 = (length - 1 - i);
					byte[] tmp127_120 = data;
					tmp127_120[tmp127_126] = (byte) (tmp127_120[tmp127_126] & 0xF);
					int tmp141_140 = (length - 1 - i);
					byte[] tmp141_134 = data;
					tmp141_134[tmp141_140] = (byte) (tmp141_134[tmp141_140] + b);
					break;
				case 1:
					int tmp154_152 = i;
					byte[] tmp154_151 = data;
					tmp154_151[tmp154_152] = (byte) (tmp154_151[tmp154_152] & 0xF);
					int tmp164_162 = i;
					byte[] tmp164_161 = data;
					tmp164_161[tmp164_162] = (byte) (tmp164_161[tmp164_162] + b);
					break;
				default:
					throw new Exception("转换错误");
				}

				b = 0;
				if (index >= 0) {
					if ((bytes[index] >= 48) && (bytes[index] <= 57)) {
						b = bytes[index] - 48 << 4;
						index--;
					} else {
						throw new Exception("转换错误");
					}
				}
				switch (alignment) {
				case 0:
					int tmp271_270 = (length - 1 - i);
					byte[] tmp271_264 = data;
					tmp271_264[tmp271_270] = (byte) (tmp271_264[tmp271_270] + b);
					break;
				case 1:
					int tmp284_282 = i;
					byte[] tmp284_281 = data;
					tmp284_281[tmp284_282] = (byte) (tmp284_281[tmp284_282] + b);
					break;
				default:
					throw new Exception("转换错误");
				}
			}
		}

		return data;
	}

	public static byte[] getBytes(String value, String charsetName)
			throws Exception {
		byte[] data = (byte[]) null;

		if ((value != null) && (value.length() > 0)) {
			if ("unicode".equalsIgnoreCase(charsetName)) {
				data = value.getBytes("unicodebig");
			} else {
				data = value.getBytes(charsetName);
			}
			if ((data != null) && ("unicode".equalsIgnoreCase(charsetName))) {
				byte[] temp = new byte[data.length - 2];
				System.arraycopy(data, 2, temp, 0, temp.length);
				data = temp;
			}
		}

		return data;
	}

	public static String getString(byte[] data, String charsetName)
			throws Exception {
		String value = null;

		if ((data != null) && (data.length > 0)) {
			if ("unicode".equalsIgnoreCase(charsetName)) {
				if ((data[0] != -2) || (data[1] != -1)) {
					byte[] data0 = new byte[data.length + 2];
					data0[0] = -2;
					data0[1] = -1;
					System.arraycopy(data, 0, data0, 2, data.length);
					value = new String(data0, charsetName);
				} else {
					value = new String(data, charsetName);
				}
			} else {
				value = new String(data, charsetName);
			}
		}

		return value;
	}

	public static String getString(byte[] data, int offset, int length,
			String charsetName) throws Exception {
		String value = null;

		if ((data != null) && (data.length > 0)) {
			if ((data[offset] != -2) || (data[(offset + 1)] != -1)) {
				byte[] data0 = new byte[length + 2];
				data0[0] = -2;
				data0[1] = -1;
				System.arraycopy(data, offset, data0, 2, length);
				value = new String(data0, charsetName);
			} else {
				value = new String(data, offset, length, charsetName);
			}
		}

		return value;
	}
}